package com.wll.demo.websocket.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: Wanglulu
 * @Date: 2020/12/30 13:40
 * @Version 0.0.1
 */
@RestController
public class WebSocketController {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(WebSocketController.class);

    @Autowired
    WebSocket webSocket;

    @GetMapping("/sendTo")
    public String sendTo(@RequestParam("userId") String userId, @RequestParam("msg") String msg) {
        try {
            webSocket.sendMessageTo(msg, userId);
        } catch (Exception e) {
            logger.info("推送失败。。。。。。。");
            return "推送失败";
        }
        return "推送成功";
    }

    @GetMapping("/sendAll")
    public String sendAll(@RequestParam("msg") String msg) {
        if(!StringUtils.hasLength(msg)){
            return "内容不能空";
        }
        try {
            String fromUserId = "system";//其实没用上
            webSocket.sendMessageAll(msg, fromUserId);
        } catch (Exception e) {
            logger.info("推送失败。。。。。。。");
            return "推送失败";
        }
        return "推送成功";
    }
}
